package com.rjpacket.supernews.app.task;

import android.os.AsyncTask;
import com.rjpacket.supernews.app.Constants;
import com.rjpacket.supernews.app.fragments.FunnyFragment;
import com.rjpacket.supernews.app.tasks.DetailTaskCallOnBack;
import com.rjpacket.supernews.app.utils.HttpTool;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by WIN7 on 2015/3/13.
 */

/**
 * 娱乐新闻详情的异步任务
 */
public class FunnyNewsDetailTask extends AsyncTask<String, Integer, JSONObject> {

    private DetailTaskCallOnBack back;

    private String position;

    public FunnyNewsDetailTask(DetailTaskCallOnBack back) {
        this.back = back;
    }

    @Override
    protected JSONObject doInBackground(String... params) {
        JSONObject ret = null;

        if (params != null && params.length > 0) {

            String url = params[0];
            position = params[1];

            byte[] bytes = HttpTool.get(url);

            if (bytes != null) {
                String str = new String(bytes);

                try {
                    ret = new JSONObject(str);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }

        return ret;
    }

    @Override
    protected void onPostExecute(JSONObject jsonObject) {
        if (jsonObject != null) {
            back.callBack(jsonObject, position);
        }
    }
}
